package ace.module.tenant.core.impl.controller.rest.admin.ext;

import ace.cmp.core.model.R;
import ace.cmp.security.oauth2.resource.server.core.annotation.PreAuthorizeIsUserAuthenticated;
import ace.module.account.api.model.dto.IamAccountDetailDto;
import ace.module.tenant.api.constant.TenantConstant;
import ace.module.tenant.api.model.input.TenTenantAccountSearchInput;
import ace.module.tenant.core.impl.service.TenantAccountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "租户账户-管理-API")
@PreAuthorizeIsUserAuthenticated
@AllArgsConstructor
@RestController
@RequestMapping(TenTenantAccountExtAdminController.PATH)
@Valid
public class TenTenantAccountExtAdminController {
  public static final String PATH = "/tenant/admin/tenant-account/v1";
  private static final String URI_ADD = "/add";
  private static final String URI_SEARCH = "/search";
  public static final String URl_ADD = PATH + URI_ADD;
  public static final String URL_SEARCH = PATH + URI_SEARCH;

  public static final String ADMIN_ACCOUNT_AUTHORITY_READ =
      TenantConstant.MODULE_NAME + "_tenant_account_read";

  public static final String ADMIN_ACCOUNT_AUTHORITY_WRITE =
      TenantConstant.MODULE_NAME + "_tenant_account_write";

  private final TenantAccountService tenantAccountService;

  @Deprecated
  @Operation(summary = "搜索列表", description = "权限:" + ADMIN_ACCOUNT_AUTHORITY_READ)
  @PreAuthorize("@securityService.hasAuthority('" + ADMIN_ACCOUNT_AUTHORITY_READ + "')")
  @GetMapping(URI_SEARCH)
  public R<Page<IamAccountDetailDto>> search(
      TenTenantAccountSearchInput input, @PageableDefault Pageable pageable) {
    Page<IamAccountDetailDto> result = tenantAccountService.search(input, pageable);
    return R.ok(result);
  }

  @Operation(summary = "添加租户账户", description = "返回id,权限:" + ADMIN_ACCOUNT_AUTHORITY_WRITE)
  @PreAuthorize("@securityService.hasAuthority('" + ADMIN_ACCOUNT_AUTHORITY_WRITE + "')")
  @PostMapping(URI_ADD)
  public R<Long> add(@Valid @RequestBody TenTenantAccountSearchInput input) {
    Long result = tenantAccountService.add(input);
    return R.ok(result);
  }
}
